//
//  WelcomeViewController.swift
//  MicroBlog
//
//  Created by QT on 15/7/31.
//  Copyright © 2015年 綦敦涛. All rights reserved.
//

import UIKit
import SDWebImage
private var lastCon: NSLayoutConstraint?

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        prepareUI()
        
        if let iconP = UserAccount.sharedAccount?.avatar_large{
            iconView.sd_setImageWithURL(NSURL(string: iconP))
        }
        
}
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        lastCon?.constant =   -(lastCon?.constant)! - UIScreen.mainScreen().bounds.height
//        lastCon?.constant = -200

        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 欢迎动画执行完成，跳转到主界面
                // TODO: 跳转主界面
                NSNotificationCenter.defaultCenter().postNotificationName(ViewControllerNotification, object: true)
        }
    }
    

    private func prepareUI(){
        view.addSubview(backGroundImg)
        view.addSubview(iconView)
        view.addSubview(labView)
        
        // 自动布局
        // 背景
        backGroundImg.ff_Fill(self.view)
//        backGroundImg.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backGroundImg]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backGroundImg]))
        
        // 头像
        let con = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(90, 90), offset: CGPointMake(0, -160))
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        // 记录最后添加的约束（头像底部的约束）
//        lastCon = view.constraints.last
        lastCon = iconView.ff_Constraint(con, attribute: NSLayoutAttribute.Bottom)
        
//        // lab约束
        
        labView.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 20))
//        labView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: labView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: labView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
    }
   // #MARK - 懒加载
    private lazy var backGroundImg:UIImageView = UIImageView(image: UIImage(named:"ad_background"))
    private lazy var iconView:UIImageView = {
        let iconV = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconV.layer.masksToBounds = true
        iconV.layer.cornerRadius = 45
        return iconV
    }()
    private lazy var labView:UILabel = {
        let lab = UILabel()
        lab.text = "欢迎回来"
        lab.textColor = UIColor.lightGrayColor()
        
        lab.font = UIFont.systemFontOfSize(15)
        
        lab.sizeToFit()
        
        return lab
    }()
    
}
